package com.riven.bilibilidemo.fragment.discover;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.bean.HotSearchBean;
import com.riven.bilibilidemo.fragment.BaseFragment;
import com.riven.bilibilidemo.interfaces.RequestJson2BeanCallBack;
import com.riven.bilibilidemo.utils.Api;
import com.riven.bilibilidemo.utils.HttpUtil;
import com.riven.bilibilidemo.utils.UIUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 发现界面
 */

public class DiscoverFragment extends BaseFragment {


    @BindView(R.id.search_scan)
    ImageView searchScan;

    @BindView(R.id.search_edit)
    TextView searchEdit;

    @BindView(R.id.search_img)
    ImageView searchImg;

    @BindView(R.id.card_view)
    CardView cardView;
    //大家都在搜 查看更多
    @BindView(R.id.tags_layout)
    TagFlowLayout tagsLayout;

    //大家都在搜 收起
    @BindView(R.id.hide_tags_layout)
    TagFlowLayout hideTagsLayout;

    @BindView(R.id.hide_scroll_view)
    NestedScrollView hideScrollView;

    @BindView(R.id.tv_more)
    TextView tvMore;

    @BindView(R.id.more_layout)
    LinearLayout moreLayout;

    @BindView(R.id.ic_quanzi_layout)
    RelativeLayout icQuanziLayout;

    @BindView(R.id.topic_center_layout)
    RelativeLayout topicCenterLayout;

    @BindView(R.id.activity_center_layout)
    RelativeLayout activityCenterLayout;

    @BindView(R.id.layout_original)
    RelativeLayout layoutOriginal;

    @BindView(R.id.layout_all_rank)
    RelativeLayout layoutAllRank;

    @BindView(R.id.layout_game_center)
    RelativeLayout layoutGameCenter;

    @BindView(R.id.layout_shop)
    RelativeLayout layoutShop;

    private boolean isShowMore = true;

    private List<HotSearchBean.ListBean> hiteTagList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutID(), container, false);
        initViews(view);
        initData();
        ButterKnife.bind(this, view);
        setTagLayOut();

        return view;
    }

    private void setTagLayOut() {
        HttpUtil.newInstance().getRequest(Api.DISCOVER_HOT_URL, HotSearchBean.class, new RequestJson2BeanCallBack<HotSearchBean>() {

            @Override
            public void onSucces(HotSearchBean hotSearchBean) {
                hiteTagList = hotSearchBean.getList();
                initTagsLayout();
            }

            @Override
            public void onFail(String error) {

            }

            private void initTagsLayout() {
                List<HotSearchBean.ListBean> been = hiteTagList.subList(0, 8);
                //收起来只截取了9条数据
                tagsLayout.setAdapter(new TagAdapter<HotSearchBean.ListBean>(been) {
                    @Override
                    public View getView(FlowLayout parent, int position, HotSearchBean.ListBean listBean) {

                        TextView mTags = (TextView) LayoutInflater.from(getActivity())
                                .inflate(R.layout.layout_tags_item, parent, false);
                        mTags.setText(listBean.getKeyword());
                         /*           mTags.setOnClickListener(
                                        v -> TotalStationSearchActivity.launch(getActivity(), listBean.getKeyword()));*/
                        return mTags;

                    }
                });
                //展开看到所有的搜索标签
                hideTagsLayout.setAdapter(new TagAdapter<HotSearchBean.ListBean>(hiteTagList) {
                    @Override
                    public View getView(FlowLayout parent, int position, HotSearchBean.ListBean listBean) {

                        TextView mTags = (TextView) LayoutInflater.from(getActivity())
                                .inflate(R.layout.layout_tags_item, parent, false);
                        mTags.setText(listBean.getKeyword());
                         /*           mTags.setOnClickListener(
                                        v -> TotalStationSearchActivity.launch(getActivity(), listBean.getKeyword()));*/
                        return mTags;
                    }
                });
            }
        });
    }

    @Override
    public int layoutID() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initViews(View view) {

    }

    @Override
    protected void initData() {

    }

    /**
     * 前往搜索界面
     */
    @OnClick(R.id.card_view)
    public void startSearchActivity() {
        startActivity(new Intent(getActivity(), TotalStationSearchActivity.class));
    }

    /**
     * 前往话题中心
     */
    @OnClick(R.id.topic_center_layout)
    public void startTopicCenterActivity() {
        startActivity(new Intent(getActivity(), TopicCenterActivity.class));
    }
    /**
     * 前往活动中心
     */
    @OnClick(R.id.activity_center_layout)
    public void startActivityCenterActivity() {
        startActivity(new Intent(getActivity(), ActivityCenterActivity.class));
    }

    /**
     * 查看更多 或者 收起
     */
    @OnClick(R.id.more_layout)
    public void showAndHideMoreLayout() {
        if (isShowMore) {
            isShowMore = false;
            hideScrollView.setVisibility(View.VISIBLE);
            tvMore.setText("收起");
            tagsLayout.setVisibility(View.GONE);
            Drawable upDrawable = getResources().getDrawable(R.drawable.ic_arrow_up_gray_round);
            upDrawable.setBounds(0, 0, upDrawable.getMinimumWidth(), upDrawable.getMinimumHeight());
            tvMore.setCompoundDrawables(upDrawable, null, null, null);
        } else {
            isShowMore = true;
            hideScrollView.setVisibility(View.GONE);
            tvMore.setText("查看更多");
            tagsLayout.setVisibility(View.VISIBLE);
            Drawable downDrawable = getResources().getDrawable(R.drawable.ic_arrow_down_gray_round);
            downDrawable.setBounds(0, 0, downDrawable.getMinimumWidth(), downDrawable.getMinimumHeight());
            tvMore.setCompoundDrawables(downDrawable, null, null, null);
        }
    }

}
